const app = getApp();
Page({
  data:{
      baseUrl: app.globalData.baseUrl,
      fileUrl: app.globalData.fileUrl,
      trainId:0,
      courseId:0,
      examId:0,
      sapId:'',
      signPath:[]
  },
  init(data){
      const width = data[0].width;
      const height=data[0].height;
      const canvas = data[0].node;
      const ctx = canvas.getContext('2d');
      const dpr = wx.getSystemInfoSync().pixelRatio
      canvas.width = width * dpr
      canvas.height = height * dpr
      ctx.scale(dpr,dpr)
      this._dpr = dpr
      this._ctx = ctx
      this._canvas = canvas
  },
  touchStart(e){
      const { _ctx:ctx } = this
      const { clientX:x ,clientY:y } = e.touches[0]
      ctx.beginPath()
  },    
  touchMove(e){
      const { _ctx:ctx } = this
      const { clientX:x ,clientY:y } = e.touches[0]
      this.data.signPath.push([x,y])
      this.setData({
          signPath:this.data.signPath
      })
      ctx.lineTo(x, y)
      ctx.stroke()
  },
  //重绘
  reset(){
      const { _ctx:ctx,_canvas:canvas } = this
      this.setData({
          signPath:[]
      })
      ctx.clearRect(10, 10, canvas.width, canvas.height)
  },
  //提交签名图片
  sure(){
      if(this.data.signPath.length <= 0){
          wx.showToast({
            title: '签名不能为空',
            icon:'none'
          })
          return 
      }
      wx.showLoading({  
        title: '签名上传中', // 提示的内容  
        mask: true, // 是否显示透明蒙层，防止触摸穿透  
      });
      //导出图片
      this.outImage()
  },
  outImage(){
      const { _canvas:canvas,_dpr:dpr } = this
      let that=this;
      wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          width:canvas.width / dpr,
          height:canvas.height / dpr,
          destWidth:canvas.width,
          destHeight:canvas.height,
          canvas:canvas,
          success(res) {
            let tempFilePath=res.tempFilePath;
            that.uploadFile(that,tempFilePath);
          },
          complete(e){
              //console.log(e)
          }
      },this)
  },
  uploadFile(that,path){
    wx.uploadFile({
      url:that.data.fileUrl+'/common/picUpload', 
      filePath: path,  
      name: 'file',
      success (res){
        console.log(res)
        let fileInfo= JSON.parse(res.data);
        that.saveSignnature(that,fileInfo.fileName)
      },
      fail (err){
        wx.hideLoading();
        wx.showToast({  
          title: '图片上传失败',  
          icon: 'none',  
          duration: 3000  
        });  
      }
    });
  },
  saveSignnature:function(that,fileName){
     let data={ 
       sapId:that.data.sapId,
       courseId:that.data.courseId,
       trainId:that.data.trainId,
       examId:that.data.examId,
       type:3,
       images:[fileName]
     };
    wx.request({
      url: that.data.baseUrl+'/staff/course/exam/sign',
      method: 'POST',
      data:data,
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        if(res.data.code!=200){
          wx.showToast({  
            title: res.data.msg,  
            icon: 'none',  
            duration: 3000  
          });  
          wx.hideLoading();
          return;
        }
        wx.redirectTo({  
          url: '/pages/my-exam-list/my-exam-list'
        })
      },fail(res){
          wx.hideLoading();
          wx.showToast({  
            title: '签名保存失败，请联系管理人员！',  
            icon: 'none',  
            duration: 3000  
          });  
      }
    });
  },

  onLoad(options){
       this.setData({
         trainId:options.trainId,
         courseId:options.courseId,
         examId:options.examId,
         sapId:options.sapId
       })
      wx.createSelectorQuery()
      .select('#myCanvas')
      .fields({node: true,size: true})
      .exec(this.init.bind(this))
  }
})
